package cn.yuan.xiaoyu.commonmodule.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by yukuoyuan on 2017/9/4.
 */
public abstract class BaseRecycleviewAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {
    private List<T> data;

    public BaseRecycleviewAdapter(List<T> data) {
        this.data = data;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(), parent, false);
        return new BaseViewHolder(view);
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        bindData(holder, position);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    /**
     * 获取子item
     *
     * @return
     */
    public abstract int getLayoutId();

    /**
     * 绑定数据
     *
     * @param holder   具体的viewHolder
     * @param position 对应的索引
     */
    protected abstract void bindData(BaseViewHolder holder, int position);

    /**
     * 刷新数据
     *
     * @param datas
     */
    public void setData(List<T> datas) {
        this.data.clear();
        this.data.addAll(datas);
        notifyDataSetChanged();
    }

    /**
     * 添加数据
     *
     * @param datas
     */
    public void addData(List<T> datas) {
        this.data.addAll(datas);
        notifyDataSetChanged();
    }
}
